﻿using System;
using System.IO;
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;


namespace SymbolicLinkManageCore
{
    public partial class Tools
    {
        public static void Explorer(string Path, bool EnableSelect = false)
        {
            if (EnableSelect)
                System.Diagnostics.Process.Start("Explorer", $"/select, \"{Path}\"");
            else
                System.Diagnostics.Process.Start("Explorer", $"/e, \"{Path}\"");
        }

        public static bool OpenFolderDialog(System.Windows.Window window,string InitialDirectory, string Title, bool IsIsFolder, out string ResultPath)
        {
            ResultPath = string.Empty;
            using (CommonOpenFileDialog dialog = new CommonOpenFileDialog())
            {
                dialog.InitialDirectory =  Path.Exists(InitialDirectory) ? InitialDirectory : Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
                dialog.IsFolderPicker = IsIsFolder;
                dialog.EnsurePathExists = true;
                dialog.EnsureFileExists = true;
                if (dialog.ShowDialog(window) == CommonFileDialogResult.Cancel)
                    return false;
                ResultPath = dialog.FileName;
            }
            return true;
        }
        public static bool OpenNullFolderDialog(System.Windows.Window window, string InitialDirectory, string Title, string DefaultFileName, out string ResultPath)
        {
            ResultPath = string.Empty;
            using (CommonOpenFileDialog dialog = new CommonOpenFileDialog())
            {
                dialog.InitialDirectory = Path.Exists(InitialDirectory) ? InitialDirectory : Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
                dialog.IsFolderPicker = false;
                dialog.EnsurePathExists = false;
                dialog.EnsureFileExists = false;
                dialog.EnsureValidNames = false;
                dialog.DefaultFileName = DefaultFileName;
                if (dialog.ShowDialog(window) == CommonFileDialogResult.Cancel)
                    return false;
                ResultPath = dialog.FileName;
            }
            return true;
        }
        public static bool OpenFileDialog(System.Windows.Window window, string Title, string defaultExt, string filter, out string ResultPath, bool DereferenceLinks = true)
        {
            ResultPath = string.Empty;
            OpenFileDialog openFileDialog = new OpenFileDialog()
            {
                AddExtension = true,
                CheckFileExists = true,
                DefaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer),
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer),
                DefaultExt = defaultExt,
                DereferenceLinks = DereferenceLinks,
                Filter = filter,
                Multiselect = false,
                Title = Title
            };
            if (!(openFileDialog.ShowDialog(window) ?? false))
                return false;
            ResultPath = openFileDialog.FileName;
            return true;
        }
        public static bool OpenFolderDialog(string Title, out string ResultPath)
        {
            ResultPath = string.Empty;
            OpenFolderDialog ofd = new OpenFolderDialog();
            ofd.Title = "";
            ofd.Multiselect = false;
            ofd.DefaultDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
            ofd.ShowHiddenItems = true;
            ofd.ValidateNames = false;

            if (!(ofd.ShowDialog() ?? false))
                return false;
            ResultPath = ofd.FolderName;
            return true;
        }
    }
}
